package com.example.myapplication.ui.study;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentNotificationsBinding;
import com.example.myapplication.databinding.FragmentStudyBinding;
import com.example.myapplication.ui.notifications.NotificationsViewModel;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceFactory;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

public class StudyFragment extends Fragment {

    private PlayerView playerView;
    private SimpleExoPlayer exoPlayer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        playerView = view.findViewById(R.id.player_view);

        // 1. 初始化ExoPlayer
        exoPlayer = new SimpleExoPlayer.Builder(requireContext()).build();
        playerView.setPlayer(exoPlayer);

        // 2. 准备播放器
        String videoUrl = "http://dev-lms-oss.oss-cn-shanghai.aliyuncs.com/files/course/20231107/6ada51ed-2521-dca1-72c6-2b93a81c5bd7.mp4"; // 替换为你的视频流URL


        MediaSource.Factory mediaSourceFactory =
                new DefaultMediaSourceFactory(getActivity());

        MediaSource mediaSource =  mediaSourceFactory.createMediaSource(MediaItem.fromUri(videoUrl));
        exoPlayer.setMediaSource(mediaSource);
        exoPlayer.prepare();
        exoPlayer.play(); // 3. 播放视频
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 4. 释放资源
        if (exoPlayer != null) {
            exoPlayer.release();
            exoPlayer = null;
        }
    }
}